﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SqlDataAccess;

namespace QuanLyBanHang
{
    public partial class F950_Bao_cao_doanh_thu_theo_khach_hang : Form
    {
        public F950_Bao_cao_doanh_thu_theo_khach_hang()
        {
            InitializeComponent();
            set_define_events();
        }

        #region Members
        BKIQuanLyBanHangEntities db = new BKIQuanLyBanHangEntities();
        #endregion

        #region Private Methods
        private void set_define_events()
        {
            this.Load += F950_Bao_cao_doanh_thu_theo_khach_hang_Load;
            this.m_cmd_xem_bao_cao.Click += m_cmd_xem_bao_cao_Click;
            this.m_txt_loc_du_lieu.TextChanged += m_txt_loc_du_lieu_TextChanged;
        }

        private void load_data_to_grid()
        {
            //var v_lst_chi_tiet_hoa_don_ban = db.GD_CT_HOA_DON_BAN
            //    .Where(x =>
            //        (x.GD_HOA_DON_BAN.NGAY_BAN >= m_dtp_tu_ngay.Value
            //            && x.GD_HOA_DON_BAN.NGAY_BAN <= m_dtp_den_ngay.Value)
            //            && (x.GD_HOA_DON_BAN.DM_KHACH_HANG.MA_KHACH_HANG.Contains(m_txt_loc_du_lieu.Text)
            //            || x.GD_HOA_DON_BAN.DM_KHACH_HANG.TEN_KHACH_HANG.Contains(m_txt_loc_du_lieu.Text)
            //            || m_txt_loc_du_lieu.Equals("")
            //            ))
            //            .ToList();
            //var v_lst_thong_ke = db.DM_KHACH_HANG
            //    .OrderBy(x => x.MA_KHACH_HANG)
            //    .ToList()
            //    .Select(x => new
            //    {
            //        MA_KHACH_HANG = x.MA_KHACH_HANG,
            //        TEN_KHACH_HANG = x.TEN_KHACH_HANG,
            //        DOANH_THU = v_lst_chi_tiet_hoa_don_ban.Where(ct => ct.GD_HOA_DON_BAN.ID_KHACH_HANG == x.ID).Sum(ct => ct.SO_LUONG * ct.DON_GIA),
            //        SO_HOA_DON = v_lst_chi_tiet_hoa_don_ban.Where(ct => ct.GD_HOA_DON_BAN.ID_KHACH_HANG == x.ID).Select(a => a.ID_HOA_DON_BAN).Distinct().Count()
            //    })
            //    .OrderBy(x => x.DOANH_THU)
            //    .ToList();
            var v_lst_chi_tiet_hoa_don_ban = db.GD_CT_HOA_DON_BAN
                .Where(x =>
                    ((x.GD_HOA_DON_BAN.DM_KHACH_HANG.MA_KHACH_HANG.Contains(m_txt_loc_du_lieu.Text)
                        || x.GD_HOA_DON_BAN.DM_KHACH_HANG.TEN_KHACH_HANG.Contains(m_txt_loc_du_lieu.Text)
                        || m_txt_loc_du_lieu.Text.Equals("")
                        && x.GD_HOA_DON_BAN.NGAY_BAN >= m_dtp_tu_ngay.Value
                        && x.GD_HOA_DON_BAN.NGAY_BAN <= m_dtp_den_ngay.Value) 
                        ))
                        .ToList();

            var v_lst_thong_ke = db.DM_KHACH_HANG
                .ToList()
                .Select(x => new
                {
                    MA_KHACH_HANG = x.MA_KHACH_HANG,
                    TEN_KHACH_HANG = x.TEN_KHACH_HANG,
                    DOANH_THU = v_lst_chi_tiet_hoa_don_ban.Where(ct => ct.GD_HOA_DON_BAN.ID_KHACH_HANG == x.ID).Sum(ct => ct.SO_LUONG * ct.DON_GIA),
                    SO_HOA_DON = v_lst_chi_tiet_hoa_don_ban.Where(ct => ct.GD_HOA_DON_BAN.ID_KHACH_HANG == x.ID).Select(a => a.ID_HOA_DON_BAN).Distinct().Count()
                })
                .OrderByDescending(x => x.DOANH_THU)
                .ToList();
            m_grv.DataSource = v_lst_thong_ke;
            m_grv.Columns[0].HeaderText = "Mã khách hàng";
            m_grv.Columns[0].Width = 100;
            m_grv.Columns[1].HeaderText = "Tên khách hàng";
            m_grv.Columns[1].Width = 200;
            m_grv.Columns[2].HeaderText = "Doanh thu";
            m_grv.Columns[2].Width = 80;
            m_grv.Columns[3].HeaderText = "Số hóa đơn";
            m_grv.Columns[3].Width = 100;
            m_grv.Columns[3].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
        }
        #endregion

        #region Events

        private void m_cmd_xem_bao_cao_Click(object sender, EventArgs e)
        {
            try
			{
				load_data_to_grid();
			}
			catch (Exception v_e)
			{
				MessageBox.Show(v_e.ToString());
			}
        }

        private void m_txt_loc_du_lieu_TextChanged(object sender, EventArgs e)
        {
            try
            {
                load_data_to_grid();
            }
            catch (Exception v_e)
            {
                MessageBox.Show(v_e.ToString());
            }
        }

        private void m_cmd_quay_lai_Click(object sender, EventArgs e)
        {
            try
            {
                this.Close();
            }
            catch (Exception v_e)
            {
                MessageBox.Show(v_e.ToString());
            }
        }

        private void F950_Bao_cao_doanh_thu_theo_khach_hang_Load(object sender, EventArgs e)
        {
            load_data_to_grid();

			// Gán giá trị ban đầu cho datetime picker
			DateTime v_dat_dau_nam = new DateTime(DateTime.Now.Year, 1, 1);
			m_dtp_tu_ngay.Value = v_dat_dau_nam;
			m_dtp_den_ngay.Value = DateTime.Now;
        }
        #endregion
    }
}
